package com.example.yingjiguanli_demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.yingjiguanli_demo.pojo.UserRoles;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserRoleMapper extends BaseMapper<UserRoles> {
    @Select("SELECT r.role_name FROM wechat_clone.users u " +
            "JOIN wechat_clone.user_roles ur ON u.id = ur.user_id " +
            "JOIN wechat_clone.roles r ON ur.role_id = r.id " +
            "WHERE u.id = #{userId}")
    List<String> getUserPermissions(@Param("userId") Long userId);


    @Insert({
            "<script>",
            "INSERT INTO user_role (user_id, role_id) VALUES ",
            "<foreach collection='userRoles' item='role' separator=','>",
            "(#{role.userId}, #{role.roleId})",
            "</foreach>",
            "</script>"
    })
    void insertRolesBatch(@Param("userRoles") List<UserRoles> userRoles);


}
